package com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.entity.HouDamSta;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.repository.HouDamStaRepository;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.vo.HouDamStaVo;
import com.css.fxfzxczh.modules.samplingPointDataCensus.param.QueryParam;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Repository
public class HouDamStaRepositoryImpl implements HouDamStaRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void delete(String id) {
        String sql = "delete from xczh_house_damage_statistics where xsphd_id = ?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public void save(HouDamSta houDamSta) {
        String sql="insert into xczh_house_damage_statistics(id,xsphd_id,destroy_area,destroy_scale,serious_damage_area,serious_damage_scale,moderate_damage_area," +
                "moderate_damage_scale,minor_damage_area,minor_damage_scale,basically_intact_area,basically_intact_scale,total_area,structure_type,damage_index) " +
                "values(:id, :xsphdId, :destroyArea, :destroyScale, :seriousDamageArea, :seriousDamageScale, :moderateDamageArea, :moderateDamageScale, " +
                ":minorDamageArea, :minorDamageScale, :basicallyIntactArea, :basicallyIntactScale, :totalArea, :structureType, :damageIndex)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(houDamSta);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<HouDamSta> getStaResult(String id) {
        String sql="select * from xczh_house_damage_statistics where xsphd_id = ? ";
        List<HouDamSta> houDamSta = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouDamSta.class), id);
        return houDamSta;
    }

    @Override
    public List<HouDamSta> getStatistic(QueryParam queryParam) {
        StringBuilder sql = new StringBuilder("select a.xstm_id,d.* from xczh_survey_task_manage a  "+
                "right join xczh_sampling_point_data b " +
                "on a.xstm_id=b.xstm_id " +
                "right join xczh_sampling_point_house_data c " +
                "on b.id=c.xspd_id " +
                "right join xczh_house_damage_statistics d " +
                "on c.id=d.xsphd_id ");
        StringBuilder whereSql = new StringBuilder(" where a.del_flag='0' and b.del_flag='0' and c.del_flag='0'");

        if (!PlatformObjectUtils.isEmpty(queryParam.getXsemId())) {
            whereSql.append(" and a.xsem_id = '" + queryParam.getXsemId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParam.getXstmIds())) {
            String xstmIds = queryParam.getXstmIds();
            String[] split = xstmIds.split(",");
            whereSql.append(" and a.xstm_id in ('" + String.join("','", split) + "')");
        }
        /*if (!PlatformObjectUtils.isEmpty(queryParam.getXstmIds())) {
            whereSql.append(" and a.xstm_id in ('" + String.join("','", queryParam.getXstmIds()) + "')");
        }*/
        if (!PlatformObjectUtils.isEmpty(queryParam.getType())) {
            whereSql.append(" and a.type  = '" + queryParam.getType() + "'");
        }
       /* if (!PlatformObjectUtils.isEmpty(queryParam.getTaskStatus())) {
            whereSql.append(" and task_status = '" + taskVo.getTaskStatus() + "'");
        }*/
        String sqlWhole = sql.append(whereSql).toString();
        List<HouDamSta> list = jdbcTemplate.query(sqlWhole, new BeanPropertyRowMapper<>(HouDamSta.class));
        return list;
    }

    @Override
    public HouDamStaVo getTotalArea(QueryParam queryParam) {
        StringBuilder sql = new StringBuilder("select sum(cast(destroy_area as DECIMAL)) destroyArea, " +
                "sum(cast(serious_damage_area as DECIMAL)) seriousDamageArea, " +
                "sum(cast(moderate_damage_area as DECIMAL)) moderateDamageArea," +
                "sum(cast(minor_damage_area as DECIMAL)) minorDamageArea," +
                "sum(cast(basically_intact_area as DECIMAL)) basicallyIntactArea from xczh_survey_task_manage a  "+
                "right join xczh_sampling_point_data b " +
                "on a.xstm_id=b.xstm_id " +
                "right join xczh_sampling_point_house_data c " +
                "on b.id=c.xspd_id " +
                "right join xczh_house_damage_statistics d " +
                "on c.id=d.xsphd_id");
        StringBuilder whereSql = new StringBuilder(" where a.del_flag='0' and b.del_flag='0' and c.del_flag='0'");

        if (!PlatformObjectUtils.isEmpty(queryParam.getXsemId())) {
            whereSql.append(" and a.xsem_id = '" + queryParam.getXsemId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParam.getXstmIds())) {
            String xstmIds = queryParam.getXstmIds();
            String[] split = xstmIds.split(",");
            whereSql.append(" and a.xstm_id in ('" + String.join("','", split) + "')");
        }
        if (!PlatformObjectUtils.isEmpty(queryParam.getType())) {
            whereSql.append(" and a.type  = '" + queryParam.getType() + "'");
        }
       /* if (!PlatformObjectUtils.isEmpty(queryParam.getTaskStatus())) {
            whereSql.append(" and task_status = '" + taskVo.getTaskStatus() + "'");
        }*/
        String sqlWhole = sql.append(whereSql).toString();
        HouDamStaVo totalArea = jdbcTemplate.queryForObject(sqlWhole, new BeanPropertyRowMapper<>(HouDamStaVo.class));
        return totalArea;
    }

    @Override
    public void updateById(HouDamSta houDamSta) {
        String sql="update xczh_house_damage_statistics set destroy_area = :destroyArea ,destroy_scale = :destroyScale ,serious_damage_area = :seriousDamageArea,serious_damage_scale = :seriousDamageScale, moderate_damage_area = :moderateDamageArea,moderate_damage_scale = :moderateDamageScale," +
                "minor_damage_area = :minorDamageArea ,minor_damage_scale = :minorDamageScale ,basically_intact_area = :basicallyIntactArea ,basically_intact_scale = :basicallyIntactScale ," +
                "total_area = :totalArea ,structure_type = :structureType ,damage_index = :damageIndex where id = :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(houDamSta);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        namedParameterJdbcTemplate.update(sql, source,keyHolder);
      }
}
